#!/bin/bash

CI_COMMIT_SHA=${GIT_COMMIT:=$(git log -n 1 --pretty=format:'%h')}
IMAGE_TAG="$CI_COMMIT_SHA"
DOCKER_HUB=10.3.0.171

build(){
        mvn package -Dmaven.test.skip=true
        sed -i "s/{{IMAGE_TAG}}/${IMAGE_TAG}/g" ./docker-compose.yml
        sed -i "s/{{DOCKER_HUB}}/${DOCKER_HUB}/g" ./docker-compose.yml

        echo "构建镜像"
        /usr/local/bin/docker-compose -v
        /usr/local/bin/docker-compose build discovery-server discovery-client nacos-config-client spring-gateway

        docker login --username=$DOCKER_USER --password=$DOCKER_PASSWORD $DOCKER_HUB

        echo "push到镜像中心"
        /usr/local/bin/docker-compose push discovery-server discovery-client nacos-config-client spring-gateway

        echo "remove本地镜像"
        if [ `docker images|grep 260219`="" ]; then
             echo "none images 260219"
        else
             docker rmi $(docker images |grep 260219 |awk '{print $3}')
        fi

}
deploy_test(){
        sed -i "s/{{DOCKER_HUB}}/${DOCKER_HUB}/g" ./yaml/spring-cloud-alibaba-deployment.yaml
        sed -i "s/{{TAG}}/${IMAGE_TAG}/g" ./yaml/spring-cloud-alibaba-deployment.yaml
        echo "发布镜像到k8s上"
        kubectl apply -f ./yaml/spring-cloud-alibaba-deployment.yaml
        kubectl apply -f ./yaml/spring-cloud-alibaba-service.yaml


}


#根据输入参数，选择执行对应方法，不输入则执行使用说明
case $1 in
 build|deploy_test )
   $1;;
 * )
   echo "not support!!! example: go <test|build|deploy>"
   exit 1;;
esac
